package com.kimehwa.studyboot.controller;

import com.kimehwa.studyboot.bean.User;
import com.kimehwa.studyboot.exception.ExceptionCodeEnum;
import com.kimehwa.studyboot.exception.Result;
import com.kimehwa.studyboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
public class HelloController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    @CrossOrigin(origins = "*",maxAge = 3600)
    public String hello(@RequestParam("id")String id){
        String hello = "hello"+id;
        log.error("发生什么错误了:{},错误是hello", hello);
        return hello;
    }

    @PostMapping("insertUser1")
    public Result<Boolean> insertUser(@RequestBody User user){
        if (user == null) {
            return Result.error(ExceptionCodeEnum.EMPTY_PARAM);
        }
        if (user.getId() == null || user.getId() <= 0) {
            return Result.error("id为空或小于0");
        }
        if (StringUtils.isEmpty(user.getName()) || user.getName().length() > 4) {
            return Result.error("姓名不符合规范");
        }
        if (user.getAge() < 18) {
            return Result.error("年龄不小于18");
        }
        if (StringUtils.isEmpty(user.getPhone()) || user.getPhone().length() != 11) {
            return Result.error("手机号码不正确");
        }

        return Result.success(userService.save(user));
    }

    @GetMapping("/{category:\\d}")
    public String findUser(@PathVariable("category") int category){
        System.out.println(category);
        return ""+ category;
    }






}
